package org.lgiavedoni.android.franq;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.lgiavedoni.android.franq.core.bean.WifiNetwork;
import org.lgiavedoni.android.franq.core.envent.dao.EventDao;
import org.lgiavedoni.android.franq.core.log.Log;
import org.lgiavedoni.android.franq.core.log.dao.LogDAO;
import org.lgiavedoni.android.franq.core.place.Place;
import org.lgiavedoni.android.franq.core.place.dao.PlaceDAO;
import org.lgiavedoni.android.franq.service.db.DbService;
import org.lgiavedoni.android.franq.service.log.Logger.LogLevel;

public class GenerateDataTest {

	private static int today = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);

	public static void create() {
		DbService.inst.deleteDataBase();

		// Places

		Place office = new Place(100, 100);
		office.setName("Office");
		List<WifiNetwork> l = new ArrayList<WifiNetwork>();
		l.add(new WifiNetwork("a", "00:0c:f6:72:6e:d8"));
		l.add(new WifiNetwork("b", "22:39:dc:64:39:b2"));
		l.add(new WifiNetwork("c", "00:1f:3f:a1:4b:4e"));
		l.add(new WifiNetwork("d", "00:23:f8:b2:5e:58"));

		office.setNetworks(l);

		Place gym = new Place(200, 200);
		gym.setName("Gym");

		PlaceDAO.inst.add(office);
		PlaceDAO.inst.add(gym);

		// MONDAY
		addIn(getDate(Calendar.MONDAY, 9, 0), office);
		addOut(getDate(Calendar.MONDAY, 18, 05), office);

		// TUESDAY
		addIn(getDate(Calendar.TUESDAY, 9, 15), office);
		addOut(getDate(Calendar.TUESDAY, 17, 50), office);

		addIn(getDate(Calendar.TUESDAY, 20, 10), gym);
		addOut(getDate(Calendar.TUESDAY, 21, 15), gym);

		// WEDNESDAY
		addIn(getDate(Calendar.WEDNESDAY, 9, 20), office);
		addOut(getDate(Calendar.WEDNESDAY, 18, 35), office);

		// THURSDAY
		addIn(getDate(Calendar.THURSDAY, 8, 50), office);
		addOut(getDate(Calendar.THURSDAY, 19, 05), office);

		addIn(getDate(Calendar.THURSDAY, 20, 0), gym);
		addOut(getDate(Calendar.THURSDAY, 21, 10), gym);

		// FRIDAY
		addIn(getDate(Calendar.FRIDAY, 9, 5), office);
		addOut(getDate(Calendar.FRIDAY, 18, 40), office);

		// Logs
		LogDAO.inst.add(new Log(LogLevel.ERROR, "Test error log"));

	}

	private static void addIn(Date d, Place place) {
		EventDao.inst.add(new MockCheckingEvent(d, place));
		MockPermanenceEvent mp = new MockPermanenceEvent(d, place);
		// GenericDao.inst.add(mp);
		place.addPermanence(mp);
		PlaceDAO.inst.update(place);
	}

	private static void addOut(Date d, Place place) {
		EventDao.inst.add(new MockCheckOutEvent(d, place));
		MockPermanenceEvent mp = (MockPermanenceEvent) place.getPermanences().get(place.getPermanences().size() - 1);
		mp.setEndDate(d);
		// GenericDao.inst.update(mp);
		PlaceDAO.inst.update(place);
	}

	private static Date getDate(int day, int hour, int minute) {
		Calendar coD1 = Calendar.getInstance();
		coD1.set(Calendar.DAY_OF_YEAR, today - 7);
		coD1.getTime();

		coD1.set(Calendar.DAY_OF_WEEK, day);
		coD1.set(Calendar.HOUR_OF_DAY, hour);
		coD1.set(Calendar.MINUTE, minute);
		return coD1.getTime();
	}

}
